/*
	       File: FileMove.cs
	Create Date: 2007-08-13
	    Version:

	Description:


	 Change log:


*/
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

using GuerillaProgrammer.Components;
using GuerillaProgrammer.Components.Logging;
using GuerillaProgrammer.FileProcessor;
using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor.Activities
{
    public partial class FileMove : Activity
    {
        #region Private member fields
        private string _DestinationFilePath;
        private string _SourceFilePath;
        #endregion

        #region Constructors and cleanup methods
        public FileMove()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        #endregion

        #region Class properties
        public static DependencyProperty DestinationFilePathProperty = System.Workflow.ComponentModel.DependencyProperty.Register("DestinationFilePath", typeof(string), typeof(FileMove));
        [Description("Destination for the workitem file.  If in file mode the path needs to be complete including a filename.\nIn directory mode the path will be have the workitem's filename appended to it.")]
        [Category("File Move")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string DestinationFilePath
        {
            get { return _DestinationFilePath; }
            set { _DestinationFilePath = value; }
        }

        public static DependencyProperty SourceFilePathProperty = System.Workflow.ComponentModel.DependencyProperty.Register("SourceFilePath", typeof(string), typeof(FileMove));
        [Description("Source for the workitem file.  If in file mode the path needs to be complete including a filename.\nIn directory mode the path will be have the workitem's filename appended to it.")]
        [Category("File Move")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string SourceFilePath
        {
            get { return _SourceFilePath; }
            set { _SourceFilePath = value; }
        }
        #endregion

        #region Events handlers
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Logger.LogEnter();

            IProcessorDataService dataService = executionContext.GetService(typeof(IProcessorDataService)) as IProcessorDataService;
            if (null == dataService)
            {
                throw new InvalidOperationException("Can't get the service!");
            }

            try
            {
                WorkItem workItem = dataService.GetWorkItem();
                StringProcessor stringProcessor = new StringProcessor(new FileProcessorSymbolTable(dataService.GetConfiguration(), workItem));

                string processedSource = stringProcessor.SubstituteStringValues(_SourceFilePath);
                string processedDestination = stringProcessor.SubstituteStringValues(_DestinationFilePath);

                Logger.LogInfo("FileMove", "File move mode.\nMoving ({0}) to ({1})\n", processedSource, processedDestination);
                //Make sure the source exists
                if (true == File.Exists(processedSource))
                {
                    //Delete the destination file if it exists
                    if (true == File.Exists(processedDestination))
                    {
                        Logger.LogWarning("FileMove", "The destination file ({0}) already exists. It will be deleted.", processedDestination);
                        File.Delete(processedDestination);
                    }

                    File.Move(processedSource, processedDestination);
                }
                else
                {
                    Logger.LogWarning("FileMove", "The source file ({0}) does not exist.", processedSource);
                }
            }
            catch (Exception exc)
            {
                Logger.LogError("FileMove", "An error occured while attempting to move a file.\nError: {0}", exc.Message);
                throw exc;
            }
            Logger.LogLeave();
            return base.Execute(executionContext);
        }
        #endregion
	}
}
